﻿#region Imports
using System;
#endregion

namespace CommonUtilities.Attributes
{
    /// <summary>
    /// Specifies that the property is unique
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class UniqueAttribute : Attribute
    {
        #region Declarations
        /// <summary>
        /// Default value for IsUnique property
        /// </summary>
        private const bool defaultUnique = true;

        /// <summary>
        /// Default property name
        /// </summary>
        public static readonly string DefaultPropertyName = "IsUnique";

        /// <summary>
        /// Specifies the default instance for the <code>CommonUtilities.Attributes.UniqueAttribute</code>
        /// </summary>
        public static readonly UniqueAttribute Default = new UniqueAttribute() { IsUnique = defaultUnique };

        /// <summary>
        /// Specifies the default value for the <code>CommonUtilities.Attributes.UniqueAttribute.IsUnique</code> when property not attributed to
        /// </summary>
        public static readonly bool DefaultValueOnAbsent = !Default.IsUnique;
        #endregion Declarations

        #region Properties
        /// <summary>
        /// Specifies that the property is unique
        /// </summary>
        public bool IsUnique { get; private set; }
        #endregion Properties

        #region Constructors
        /// <summary>
        /// Initializes a new default instance of <code>CommonUtilities.Attributes.UniqueAttribute</code>
        /// </summary>
        public UniqueAttribute()
            : base()
        {
            IsUnique = defaultUnique;
        }
        #endregion Constructors

        #region Methods
        public override bool Equals(object obj)
        {
            return this.IsUnique == ((UniqueAttribute)obj).IsUnique;
        }

        /// <summary>
        /// Determines if this attribute is the default instance
        /// </summary>
        /// <returns>TRUE if the attribute is the default instance for this attribute class;
        /// <para>otherwise, FALSE</para></returns>
        public override bool IsDefaultAttribute()
        {
            return this.IsUnique == Default.IsUnique;
        }
        #endregion Methods
    }
}
